/******************************************************************************
Author: Akshay Dixit
Date created: June 24, 2020
Data last modified: June 24, 2020
File name: analysis.do
Project: T4D Indonesia & Tanzania 
Purpose: Independently reproduces the final graphs and tables for primary and secondary outcomes
******************************************************************************/

clear all
set more off

*** THIS IS THE INPUT THAT MUST BE CHANGED SPECIFIC TO THE USER/ANALYSIS ***
*gl identity ""
*gl user "" 
		   
******************************************************************************

** Creating Global File Paths **
gl data_in "$user/Indonesia/Data"
gl data_tz "$user/Tanzania/Data"
gl analysis_in "$user/Indonesia/Analysis"
gl analysis_tz "$user/Tanzania/Analysis"

******************************************************************************
*** INDONESIA ***
******************************************************************************

** Import cleaned .dta file with primary and secondary outcomes **
u "$data_in/household_data_with_main_outcomes.dta", clear

** Set working directory to analysis **
cd "$analysis_in"

***Label variables***
lab var skilled_provider_birth "Birth with a skilled provider"
lab var facility_birth "Birth at a facility"
lab var postpartum_postnatal_care "Postnatal care"
lab var z_content "Content of care"
lab var stunted "Stunting"
lab var underweight "Underweight"
lab var participation "Empowerment - Participation"
lab var ks11 "Empowerment - Vignette"

lab var number_anc_visits_binary "Four or more ANC visits"
lab var anc_first_trimester "First ANC visit within the first trimester"
lab var birth_preparedness "Birth preparedness"
lab var anc_content "Content of Antenatal Care"
lab var low_birthweight "Low birthweight"
lab var mother_k6_score "Maternal depression (K6 score)"


** Primary outcomes: Coefficient plot **

	/*
	
	A total of 8 primary outcomes are analyzed above. 
	
	1. Six of them are not standardized.
	skilled_provider_birth facility_birth postpartum_postnatal_care stunted underweight ks11
	
	2. Two of them are standardized
	participation z_content
	
	*/

***Non-standardized outcomes***

local outcomes skilled_provider_birth facility_birth postpartum_postnatal_care stunted underweight ks11 
foreach var of local outcomes {
	
	*Dividing by standard deviation of control group, to create a transformed outcome
	qui sum `var' if treatment == 0
	local sd_`var' = r(sd)
	g `var'_sd = `var'/r(sd)
	
	*Running regression on the transformed outcome
	qui reg `var'_sd treatment strata1 strata2 strata3, vce(cluster iddesa)
	estimates store e_`var'
}

***Standardized outcomes***
	
local outcomes participation z_content
foreach var of local outcomes {
	qui reg `var' treatment strata1 strata2 strata3, vce(cluster iddesa)
	estimates store e_`var'
}	

#delimit ;
coefplot (e_skilled_provider_birth, aseq("Birth with a skilled provider") 
		\ e_facility_birth, aseq("Birth at a facility")
		\ e_postpartum_postnatal_care, aseq("Postnatal care")
		\ e_z_content, aseq("Content of care")
		\ e_stunted, aseq("Stunting")
		\ e_underweight, aseq("Underweight")
		\ e_participation, aseq("Empowerment - Participation")
		\ e_ks11, aseq("Empowerment - Vignette"))
	, keep(treatment) xline(0) swapnames title("Indonesia", size(medium))
	  note("Notes: Displaying 95% confidence intervals" "Higher values represent 'better' outcomes, except for stunting and underweight", size(vsmall)) 
	  xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.4(0.1)0.4) scheme(s2mono)
;	

#delimit cr

graph export "primary_outcomes_in.png", as(png) replace

******************************************************************************

** Secondary outcomes **

cap erase "secondary_outcomes_in.xls"

local outcomes low_birthweight mother_k6_score birth_preparedness number_anc_visits_binary anc_first_trimester anc_content 
foreach var of local outcomes {
	sleep 2000
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	
	qui reg `var' treatment strata1 strata2 strata3, vce(cluster iddesa)
	
	outreg2 using "secondary_outcomes_in.xls", append label nonote nor2 keep(treatment) ///
	addtext(Control Mean, "`rounded'") ///
	addnote("Notes: Robust standard errors clustered at the village-level in parentheses.", "All regressions include strata-specific binary variables.", "*** p<0.01, ** p<0.05, * p<0.1")
}

cap erase "secondary_outcomes_in.txt"

clear

******************************************************************************
*** TANZANIA ***
******************************************************************************

** Import cleaned .dta file with primary and secondary outcomes **
u "$data_tz/T4D_end_hh_with_main_outcomes.dta", clear

** Set working directory to analysis **
cd "$analysis_tz"

** Randomization strata **
gl strata = "strata1 strata2 strata3 strata4 strata5 strata6 strata7"

** Label variables.**
lab var anc_first_trimester "ANC visit within first trimester" 
lab var number_anc_visits_binary "Four or more ANC visits"
lab var skilled_provider_birth "Birth with a skilled provider"
lab var facility_birth "Birth at a facility"
lab var postpartum_postnatal_care "Postnatal care"
lab var z_content "Content of care"
lab var stunted "Stunting"
lab var underweight "Underweight"
lab var participation "Empowerment - Participation"
lab var ks11 "Empowerment - Vignette"

lab var birth_preparedness "Birth preparedness"
lab var low_birthweight_strict_impute "Low birthweight"
lab var mother_k6_score "Maternal depression (K6 score)"
lab var treatment "Treatment"

** Primary outcomes: Coefficient plot **

	/*
	
	A total of 10 primary outcomes are analyzed above. 
	
	1. Eight of them are not standardized.
	anc_first_trimester number_anc_visits_binary skilled_provider_birth facility_birth postpartum_postnatal_care stunted underweight ks11
	
	2. Two of them are standardized
	participation z_content
	
	*/

***Non-standardized outcomes***

local outcomes anc_first_trimester number_anc_visits_binary skilled_provider_birth facility_birth postpartum_postnatal_care stunted underweight ks11 
foreach var of local outcomes {
	
	*Dividing by standard deviation of control group, to create a transformed outcome
	qui sum `var' if treatment == 0
	local sd_`var' = r(sd)
	g `var'_sd = `var'/r(sd)
	
	*Running regression on the transformed outcome
	qui reg `var'_sd treatment $strata, vce(cluster facility_id) 
	estimates store e_`var'
}

***Standardized outcomes***
	
local outcomes participation z_content
foreach var of local outcomes {
	qui reg `var' treatment $strata, vce(cluster facility_id) 
	estimates store e_`var'
}	

#delimit ;
coefplot (e_anc_first_trimester, aseq("ANC visit within first trimester")
		\ e_number_anc_visits_binary, aseq("Four or more ANC visits")
		\ e_skilled_provider_birth, aseq("Birth with a skilled provider") 
		\ e_facility_birth, aseq("Birth at a facility")
		\ e_postpartum_postnatal_care, aseq("Postnatal care")
		\ e_z_content, aseq("Content of care")
		\ e_stunted, aseq("Stunting")
		\ e_underweight, aseq("Underweight")
		\ e_participation, aseq("Empowerment - Participation")
		\ e_ks11, aseq("Empowerment - Vignette"))
	, keep(treatment) xline(0) swapnames title("Tanzania", size(medium))
	  note("Note: Displaying 95% confidence intervals" "Higher values represent 'better' outcomes, except for stunting and underweight", size(vsmall)) 
	  xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.4(0.1)0.4) scheme(s2mono)
;	

#delimit cr

graph export "primary_outcomes_tz.png", as(png) replace

******************************************************************************

** Secondary outcomes **

cap erase "secondary_outcomes_tz.xls"

local outcomes low_birthweight_strict_impute mother_k6_score birth_preparedness 
foreach var of local outcomes {
	sleep 2000
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	
	qui reg `var' treatment $strata, vce(cluster facility_id)
	
	outreg2 using "secondary_outcomes_tz.xls", append label nonote nor2 keep(treatment) ///
	addtext(Control Mean, "`rounded'") ///
	addnote("Notes: Robust standard errors clustered at the facility-level in parentheses.", "All regressions include strata-specific binary variables.", "*** p<0.01, ** p<0.05, * p<0.1")
}

cap erase "secondary_outcomes_tz.txt"

clear

******************************************************************************




